//
//  HomeTableHeaderView.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/8/28.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "HomeTableHeaderView.h"
#import "XSQBanner.h"
#import "BannerModel.h"
#import "NotificationListModel.h"
#import "HomeTypeCollectionViewCell.h"
#import "TypeListModel.h"
#import "BFHomeNoticesView.h"
#import "HomeNewsDetailController.h"// 广播详情
#import "RecommendFriendsController.h"// 邀请好友
#import "TutorialsController.h"// 投资教程
#import "RiskProtectionController.h"// 风险保障

static NSString * const cellID = @"HomeTypeCollectionViewCell";
@interface HomeTableHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource,BFHomeNoticesViewDelegate>
@property (nonatomic,strong) XSQBanner *banner;//
@property (nonatomic,strong) NSMutableArray *bannerArr;//
@property (weak, nonatomic) IBOutlet UIView *broadcastBgView;
//@property (nonatomic,strong) TextScrollsUpAndDownView *textView;//
@property (nonatomic,strong) NSMutableArray *dataSourceArr;//
@property (nonatomic,strong) NSMutableArray *contArray;

@property (weak, nonatomic) IBOutlet UIView *typeBgView;
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *typeArray;

@property (nonatomic,strong) BFHomeNoticesView *noticesView;

@end
@implementation HomeTableHeaderView

- (void)Assignment{
    // 获取轮播图
    self.bannerArr = [NSMutableArray arrayWithCapacity:0];
    [NetWorkTool POST:Mbannerapi param: nil success:^(id dic) {
        
        self.bannerArr = [BannerModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        NSMutableArray *mu = [NSMutableArray arrayWithCapacity:0];
        for (BannerModel *model in self.bannerArr) {
            [mu addObject:model.img];
        }
        if (self.banner) {
            [self.banner removeFromSuperview];
        }
        self.banner = [[XSQBanner alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 230)];
        self.banner.pageLocation = PageControlLocationCenter;
        [self addSubview:self.banner];
        
        UIImageView *radianImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 205, SCREEN_WIDTH, 25)];
        radianImageView.image = [UIImage imageNamed:@"轮播图圆弧"];
        radianImageView.userInteractionEnabled = YES;
        [self.banner addSubview:radianImageView];
        [self.banner updateImageArr:mu AndImageClickBlock:^(NSInteger index) {
            //        BannerModel *model = self.bannerArr[index];
            
            //        if (model.details.length == 0) {
            //
            //        } else {
            //            BannerDetailsViewController *vc = [[BannerDetailsViewController alloc] init];
            //            vc.hidesBottomBarWhenPushed = YES;
            //            vc.bannerId = model.id;
            //            vc.titleStr = model.title;
            //            vc.partsId = model.partsId;
            //            [[self viewInViewController].navigationController pushViewController:vc animated:YES];
            //        }
        }];
        
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
    // 获取广播
    self.dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    self.contArray = [NSMutableArray arrayWithCapacity:0];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"type"] = @"3"; 

    [NetWorkTool POST:noticeList param:param success:^(id dic) {
        self.dataSourceArr = [NotificationListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];

        for (NotificationListModel *model in self.dataSourceArr){
            self.contArray = [[self.contArray arrayByAddingObject:model.title] mutableCopy];
        };
        if (self.noticesView) {
            [self.noticesView removeFromSuperview];
        }
        self.noticesView = [[BFHomeNoticesView alloc] initWithFrame:CGRectMake(65, 10, SCREEN_WIDTH-95, 30)];
        [self.noticesView setNoticesByTitleArr:self.contArray];
        self.noticesView.delegate = self;
        [self.broadcastBgView addSubview:self.noticesView];
        
    } other:^(id dic) {

    } fail:^(NSError *error) {

    } needUser:NO];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(100, 44);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0,0);// 上 左 下 右
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) collectionViewLayout:layout];
    self.collectionView.backgroundColor = UIColor.whiteColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeTypeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellID];
    [self.typeBgView addSubview:self.collectionView];
    
    NSString *isFirst = [USER_DEFAULT objectForKey:@"isFirst"];
    if ([isFirst isEqualToString:@"1"]) {
        [NetWorkTool POST:getTypeList param:nil success:^(id dic) {
            self.typeArray = [NSMutableArray arrayWithCapacity:0];
            self.typeArray = [TypeListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
            TypeListModel * model;
            if (self.typeArray.count>0) {
                model = self.typeArray[0];
                model.isSelected = YES;
            }
            [self.collectionView reloadData];
            if (self.didSelectedTypeClick) {
                self.didSelectedTypeClick(model.id);
            }
            NSString *isFirst = @"0";
            [USER_DEFAULT setObject:isFirst forKey:@"isFirst"];
        } other:^(id dic) {
            
        } fail:^(NSError *error) {
            
        } needUser:NO];
    }
    
}
-(void)tapNoticeTag:(NSInteger)tag{
    NotificationListModel * model;
    if (self.dataSourceArr.count > 0) {
        model = self.dataSourceArr[tag];
    }
    HomeNewsDetailController *vc = [[HomeNewsDetailController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@/lsb/homeApi/newsDetails?id=%@",HOST_URL,model.id];
       [[self viewInViewController].navigationController pushViewController:vc animated:YES];
    NSLog(@"%@",vc.urlString);
}
#pragma mark- UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.typeArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TypeListModel * model = self.typeArray[indexPath.row];
    HomeTypeCollectionViewCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.model = model;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    for (int i = 0; i<self.typeArray.count; i++) {
        TypeListModel * model = self.typeArray[i];
        if (i == indexPath.row) {
            model.isSelected = YES;
        } else {
            model.isSelected = NO;
        }
    }
    [self.collectionView reloadData];
    TypeListModel * model = self.typeArray[indexPath.row];
    if (self.didSelectedTypeClick) {
        self.didSelectedTypeClick(model.id);
    }
    
}
// 邀请好友
- (IBAction)inviteFriendsButtonClick:(UIButton *)sender {
    RecommendFriendsController * vc = [[RecommendFriendsController alloc] init];
    [[self viewInViewController].navigationController pushViewController:vc animated:YES];
}
// 投资教程
- (IBAction)investmentTutorialButtonClick:(UIButton *)sender {
    TutorialsController * vc = [[TutorialsController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@/lsb/tzjc.html",HOST_URL];
    [[self viewInViewController].navigationController pushViewController:vc animated:YES];
}
// 风险保障
- (IBAction)riskProtectionButtonClick:(UIButton *)sender {
    RiskProtectionController * vc = [[RiskProtectionController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@/lsb/fxbz.html",HOST_URL];
    [[self viewInViewController].navigationController pushViewController:vc animated:YES];
}

// 获取view所在的控制器
- (UIViewController *)viewInViewController {
    
    UIResponder *resp = [self nextResponder];
    while (resp) {
        if ([resp isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)resp;
        }
        resp = [resp nextResponder];
    }
    return nil;
}
@end
